home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / text / tex / EVPaths140.lha / TeXWhereIs2.c < prev   
C/C++ Source or Header  |  1995-05-05  |  4KB  |  179 lines

  1. /*
  2.  * TeXWhereIs2.c - a simple file to show how EVPaths.lib routines work.
  3.  *
  4.  * This program do not use any startup code.
  5.  *
  6.  * Copyright © 1994, 1995 by Giuseppe Ghibò
  7.  *
  8.  * Version 1.1 - 5 May 1995
  9.  *
  10.  * -----------------------------------------------------------------------
  11.  *
  12.  * Compile with:
  13.  * 
  14.  *    SC OPT LINK LIB EVPaths.lib NOSTKCHK NOSTARTUP TeXWhereIs2.c
  15.  * or
  16.  *
  17.  *    SC OPT NOSTKCHK TeXWhereIs2.c
  18.  *    SLINK FROM TeXWhereIs2.o TO TeXWhereIs2 LIB EVPaths.lib LIB:sc.lib
  19.  * or
  20.  *
  21.  *    SC OPT NOSTKCHK NOSTARTUP LINK TeXWhereIs2.c EVPaths.c SNPrintf.a
  22.  *
  23.  * -----------------------------------------------------------------------
  24.  *
  25.  * Usage:
  26.  *
  27.  *    TeXWhereIs2 VAR=MYVAR file1 file2 ... fileN [SHOW]
  28.  *
  29.  */
  30.  
  31. #include <exec/execbase.h>
  32. #include <workbench/startup.h>
  33.  
  34. #include <string.h>
  35.  
  36. #include <proto/dos.h>
  37. #include <proto/exec.h>
  38.  
  39. #include "evpaths.h"
  40.  
  41. #define TEMPLATE "VAR/K,FILE/A/M,SHOW/S"
  42.  
  43. enum {    OPT_VAR,
  44.     OPT_FILE,
  45.     OPT_SHOW,
  46.     OPT_COUNT };
  47.  
  48. extern struct ExecBase *SysBase;
  49. extern struct DosLibrary *DOSBase;
  50.  
  51. #define BUFLEN 256
  52. #define EVPBUF 8192L
  53.  
  54. STRPTR default_path = ".,TeX:texinputs**,MF:mfinputs**";
  55.  
  56. /* uncomment what follow and comment the statement above if you want to
  57.    use an array of strings as default path */
  58.  
  59. //STRPTR default_path[] = {     ".",
  60. //                "TeX:texinputs**",
  61. //                "MF:mfinputs**",
  62. //                NULL };
  63.  
  64. void __saveds Main(void)
  65. {
  66.     struct Process *me;
  67.     struct WBStartup *WBenchMsg;
  68.     struct RDArgs *rdargs;
  69.     LONG opts[OPT_COUNT];
  70.     char buf[BUFLEN];
  71.     STRPTR varname = "", *fname = NULL, s;
  72.     BOOL show = 0;
  73.     struct EnvVarPath *var;
  74.  
  75.     SysBase = *(struct ExecBase **)4;
  76.  
  77.     if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36L)) == NULL)
  78.         return;
  79.  
  80.     me = (struct Process *)SysBase->ThisTask;
  81.  
  82.     if (me->pr_CLI)
  83.     {
  84.         WBenchMsg = NULL;
  85.  
  86.         memset (opts, 0, sizeof(opts));
  87.  
  88.         if (rdargs = ReadArgs(TEMPLATE, opts, NULL))
  89.         {
  90.             int j;
  91.  
  92.             if (opts[OPT_VAR])
  93.                 varname = (STRPTR) opts[OPT_VAR];
  94.  
  95.             if (opts[OPT_FILE])
  96.                 fname = (STRPTR *) opts[OPT_FILE];
  97.  
  98.             if (opts[OPT_SHOW])
  99.                 show = 1;
  100.  
  101.             var = Alloc_EnvVarPath(varname, EVPBUF);
  102.  
  103. /* Here are some alternatives for Init_EnvVarPath() */
  104.  
  105. //            Init_EnvVarPath(var, NULL, NULL);
  106. //            Init_EnvVarPath(var, default_path, ENVPATH_DEFARR);
  107.             Init_EnvVarPath(var, default_path, ENVPATH_DEFSTR);
  108. //            Init_EnvVarPath(var, default_path, ENVPATH_DEFSTR | ENVPATH_PREPEND_PATH);
  109. //            Init_EnvVarPath(var, default_path, ENVPATH_DEFSTR | ENVPATH_APPEND_PATH);
  110.  
  111.             if (show)
  112.             {
  113.                 int i = 0;
  114.  
  115.                 Printf("The environment variable `%s' has length %ld\n", varname, GetVarLength(varname));
  116.                 while (var->storage.strings[i])
  117.                     Printf("%ld -> `%s'\n", i, var->storage.strings[i++]);
  118.             }
  119.  
  120.             j = 0;
  121.  
  122.             while (fname[j])
  123.             {
  124.                 if (s = EVP_FileSearch(fname[j], var, buf, BUFLEN))
  125.                     Printf("File found: `%s'\n",s);
  126.                 else
  127.                     Printf("File `%s' not found.\n",fname[j]);
  128.                 j++;
  129.             }
  130.  
  131. /* Here is an example to show how EVP_Open() works */
  132. /*
  133.             while (fname[j])
  134.             {
  135.                 BPTR fh;
  136.  
  137.                 if (fh = EVP_Open(fname[j], var, buf, 256, MODE_OLDFILE))
  138.                 {
  139.                     Printf("File found: `%s'\n",buf);
  140.                     Read(fh,buf,2);
  141.                     Close(fh);
  142.                     Printf("First 2 chars = '%c%c'\n",buf[0],buf[1]);
  143.                 }
  144.                 else
  145.                     Printf("File `%s' not found.\n",fname[j]);
  146.                 j++;
  147.             }
  148. */
  149.  
  150.             Free_EnvVarPath(var);
  151.             FreeArgs(rdargs);
  152.         }
  153.         else
  154.             PrintFault(IoErr(), NULL);
  155.     }
  156.     else
  157.     {
  158.         BPTR StdOut;
  159.  
  160.         WaitPort(&me->pr_MsgPort);
  161.         WBenchMsg = (struct WBStartup *)GetMsg(&me->pr_MsgPort);
  162.  
  163.         if (StdOut = Open("CON:20/20/400/80/A Window/Auto/Close/Wait", MODE_NEWFILE))
  164.         {
  165.             FPrintf(StdOut,"You must run me from CLI, not from WB!\n");
  166.             Close(StdOut);
  167.         }
  168.     }
  169.  
  170.     if (DOSBase)
  171.         CloseLibrary((struct Library *)DOSBase);
  172.  
  173.     if (WBenchMsg)
  174.     {
  175.         Forbid();
  176.         ReplyMsg(&WBenchMsg->sm_Message);
  177.     }
  178. }
  179.